<template>
  <div class="show-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>状态</span>
        <el-button
          style="margin-left: 400px; width: 100px"
          type="primary"
          @click="add('state')"
        >
          添加
        </el-button>
      </div>
      <el-table :data="stateTable">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column width="200" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleClick('state', scope.row)">
              编辑
            </el-button>
            <el-button
              slot="reference"
              type="warning"
              @click="deleteData('state', scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--    12312312312-->
    <el-card>
      <div slot="header" class="clearfix">
        <span>生态类</span>
        <el-button
          style="margin-left: 400px; width: 100px"
          type="primary"
          @click="add('category')"
        >
          添加
        </el-button>
      </div>
      <el-table :data="categoryTable">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column width="200" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="handleClick('category', scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="warning"
              @click="deleteData('category', scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card>
      <div slot="header" class="clearfix">
        <span>目</span>
        <el-button
          style="margin-left: 400px; width: 100px"
          type="primary"
          @click="add('order')"
        >
          添加
        </el-button>
      </div>
      <el-table :data="orderTable">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column width="200" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleClick('order', scope.row)">
              编辑
            </el-button>
            <el-button type="warning" @click="deleteData('order', scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card>
      <div slot="header" class="clearfix">
        <span>科</span>
        <el-button
          style="margin-left: 400px; width: 100px"
          type="primary"
          @click="add('family')"
        >
          添加
        </el-button>
      </div>
      <el-table :data="familyTable">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column width="200" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleClick('family', scope.row)">
              编辑
            </el-button>
            <el-button type="warning" @click="deleteData('family', scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <ChangeDialog ref="dialog" @resttable="restTable" />
  </div>
</template>

<script>
  import {
    getState,
    getCategory,
    getFamily,
    getOrder,
    deleteState,
    deleteCategory,
    deleteFamily,
    deleteOrder,
    addState,
  } from '@/api/bird'
  import ChangeDialog from '@/views/bird/select/components/changeDialog'

  export default {
    name: 'Select',
    components: {
      ChangeDialog,
    },
    data() {
      return {
        stateTable: [],
        categoryTable: [],
        orderTable: [],
        familyTable: [],
        state: false,
        info: {
          id: null,
          name: null,
        },
      }
    },
    created() {},
    mounted() {
      this.initState()
      this.initCategory()
      this.initOrder()
      this.initFamily()
    },
    methods: {
      //加载数据
      //state
      initState() {
        getState().then((res) => {
          this.stateTable = res.data
          console.log(this.stateTable)
        })
      },
      initCategory() {
        getCategory().then((res) => {
          this.categoryTable = res.data
        })
      },
      initOrder() {
        getOrder().then((res) => {
          this.orderTable = res.data
        })
      },
      initFamily() {
        getFamily().then((res) => {
          this.familyTable = res.data
        })
      },
      handleClick(kind, row) {
        this.info.id = row.id
        this.info.name = row.name
        this.$refs['dialog'].onOpen(this.info, 'update', kind)
        this.resetInfo()
      },
      add(kind) {
        console.log(123)
        this.$refs['dialog'].onOpen(this.info, 'add', kind)
        this.resetInfo()
      },
      deleteData(kind, row) {
        if (confirm('你确定要删除吗')) {
          switch (kind) {
            case 'state':
              this.info.id = row.id
              deleteState(this.info).then((res) => {
                this.$message.success(res.msg)
                this.resetInfo()
                this.restTable('state')
              })
              break
            case 'category':
              this.info.id = row.id
              deleteCategory(this.info).then((res) => {
                this.$message.success(res.msg)
                this.resetInfo()
                this.restTable('category')
              })
              break
            case 'order':
              this.info.id = row.id
              deleteOrder(this.info).then((res) => {
                this.$message.success(res.msg)
                this.resetInfo()
                this.restTable('order')
              })
              break
            case 'family':
              this.info.id = row.id
              deleteFamily(this.info).then((res) => {
                this.$message.success(res.msg)
                this.resetInfo()
                this.restTable('family')
              })
              break
          }
        } else {
          this.$message.info('操作取消')
        }
      },
      restTable(kind) {
        switch (kind) {
          case 'state':
            console.log('初始化页面')
            this.initState()
            break
          case 'category':
            this.initCategory()
            break
          case 'order':
            this.initOrder()
            break
          case 'family':
            this.initFamily()
            break
        }
      },
      resetInfo() {
        this.info.name = null
        this.info.id = null
      },
    },
  }
</script>
<style scoped>
  .el-card {
    width: 70%;
  }
</style>
